//
//  MyRecommandViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/29.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "MyRecommandListCell.h"
#import "MyRecommandSectionHead.h"

#import "UserOrderListViewController.h"

#import "MyRecommandViewController.h"

@interface MyRecommandViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic, assign) NSInteger pageNum;

@property (nonatomic, strong) NSMutableArray <CommonRecommandUserModel *>*dataArray;

@end

@implementation MyRecommandViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self request_recList];
}

- (void)refreshParam {
    self.pageSize = 20;
    self.pageNum = 1;
}

- (void)initView {
    
    self.title = @"我推荐的客户";
    
    [self.view addSubview:self.tableview];
    
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.tableview reloadData];
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

#pragma mark sectionheader
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    MyRecommandSectionHead *headView = [[NSBundle mainBundle] loadNibNamed:@"MyRecommandSectionHead"owner:nil options:nil].firstObject;
    
    return  headView;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MyRecommandListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyRecommandListCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    [cell fillCellWithModel:self.dataArray[indexPath.row]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UserOrderListViewController *vc = [[UserOrderListViewController alloc] init];
    vc.userId = self.dataArray[indexPath.row].userId;
    vc.userName = self.dataArray[indexPath.row].name;
    vc.userImage = self.dataArray[indexPath.row].imgUrl;

    [self.navigationController pushViewController:vc animated:YES];
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  UIColorHex(0xf1f1f1);
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerNib:[UINib nibWithNibName:@"MyRecommandListCell" bundle:nil] forCellReuseIdentifier:@"MyRecommandListCellID"];
        
        WEAKSELF
        _tableview.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
            [weakSelf refreshParam];
            [weakSelf request_recList];
        }];

        _tableview.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
            weakSelf.pageNum ++;
            [weakSelf request_recList];
        }];
        _tableview.mj_footer.ignoredScrollViewContentInsetBottom = Is_IPhoneX ? Bottom_SafeHeight : CGFLOAT_MIN;

    }
    return _tableview;
}

- (NSMutableArray<CommonRecommandUserModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

- (void)request_recList {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    NSMutableDictionary *t = @{}.mutableCopy;
    [t setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    [dic setValue:t forKey:@"t"];
    [dic setValue:@(self.pageNum) forKey:@"pageNum"];
    [dic setValue:@(self.pageSize) forKey:@"pageSize"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_GetRecList];
    
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];

        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSArray *list = [resultDic valueForKey:@"list"];
                if ([list isKindOfClass:[NSArray class]]) {
                    
                    if (weakSelf.pageNum == 1) {
                        weakSelf.dataArray = @[].mutableCopy;
                    }
                    
                    for (int i = 0; i < list.count; i++) {
                        NSDictionary *listDic = list[i];
                        CommonRecommandUserModel *model = [CommonRecommandUserModel modelWithDictionary:listDic];
                        [weakSelf.dataArray addObject:model];
                    }
                }
                [weakSelf.tableview reloadData];
                
            } else {
               [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
               [SVProgressHUD dismissWithDelay:1];
           }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
    }];
}


@end
